\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{prelude}[2020/10/06 AlgoXY Prelude LaTeX package]
%
% loading packages
%

\RequirePackage{ifpdf}
\RequirePackage{ifxetex}

\ifpdf
  \RequirePackage[pdftex,%
       bookmarksnumbered,%
              colorlinks,%
          linkcolor=blue,%
              hyperindex,%
        plainpages=false,%
       pdfstartview=FitH]{hyperref}
\else\ifxetex
  \RequirePackage[bookmarksnumbered,%
               colorlinks,%
           linkcolor=blue,%
               hyperindex,%
         plainpages=false,%
        pdfstartview=FitH]{hyperref}
\else
  \RequirePackage[dvipdfm,%
        bookmarksnumbered,%
               colorlinks,%
           linkcolor=blue,%
               hyperindex,%
         plainpages=false,%
        pdfstartview=FitH]{hyperref}
\fi\fi
%\usepackage{hyperref}

% other packages
%--------------------------------------------------------------------------
\usepackage{graphicx, color}
\usepackage{adjustbox}
\usepackage{wrapfig}
\usepackage{subcaption}  % subfig is deprecated
\usepackage{enumerate}
\usepackage{multicol}
\usepackage{pdfpages}    % to include pdf
\usepackage[table]{xcolor} % for colored table
\usepackage{hhline}
\usepackage{tikz}
\usetikzlibrary{patterns,matrix,positioning,shapes,arrows}

\usepackage{amsmath, amsthm, amssymb, amsbsy, nccmath} % for math
\usepackage{textgreek, upgreek}
\usepackage{gensymb}
\usepackage{mathtools}
\usepackage{extarrows}
\usepackage{exercise} % for exercise
\usepackage{stmaryrd} % for banana brackets
\usepackage{pict2e}   % for customize lens brackets
\usepackage{ulem}

\usepackage{import} % for nested input
\usepackage{standalone}

%
% for programming
%
\usepackage{verbatim}
\usepackage{fancyvrb}
\usepackage{listings}
\usepackage{algorithm} %to remove rules change to \usepackage[plain]{algorithm}
\usepackage[noend]{algpseudocode} %for pseudo code, include algorithmicsx automatically
\usepackage{appendix}
\usepackage{makeidx} % for index support
\usepackage{titlesec}
\usepackage{epigraph}

\usepackage{fontspec}
\usepackage{xunicode}
\usepackage{textcomp}
\usepackage{mdframed}
\usepackage{url}
\usepackage{xeCJK}
\usepackage{sourcecodepro}

\titleformat{\paragraph}
{\normalfont\normalsize\bfseries}{\theparagraph}{1em}{}
\titlespacing*{\paragraph}
              {0pt}{3.25ex plus 1ex minus .2ex}{1.5ex plus .2ex}

\newcommand\doubleplus{\ensuremath{\mathbin{+\mkern-10mu+}}}
%\newcommand\doubleplus{+\kern-1.3ex+\kern0.8ex}
%\newcommand\mdoubleplus{\ensuremath{\mathbin{+\mkern-10mu+}}}

% for literate Haskell code
\lstdefinestyle{Haskell}{
  language=Haskell,
  flexiblecolumns=false,
  numberstyle=\ttfamily\tiny,
  emphstyle={\bf},
  escapeinside={*'}{'*},
  stringstyle=\mdseries\rmfamily,
  keywordstyle=\bfseries\ttfamily, %rmfamily
  basicstyle=\footnotesize\ttfamily, %sffamily
  commentstyle = \footnotesize\rmfamily,
  showstringspaces=false,
  morecomment=[l]--,
  literate={+}{{$+$}}1 {/}{{$/$}}1 {*}{{$*$}}1 {=}{{$=$}}1
           {++}{{$\doubleplus$}}2 {/=}{{$\neq$}}2
           {>}{{$>$}}1 {<}{{$<$}}1 {\\}{{$\lambda$}}1
           {`}{{\texttt{`}}}1
           {\\\\}{{\char`\\\char`\\}}1
           {->}{{$\rightarrow$}}2 {>=}{{$\geq$}}2 {<-}{{$\leftarrow$}}2
           {<=}{{$\leq$}}2 {=>}{{$\Rightarrow$}}2
           {\ .}{{$\circ$}}2 {\ .\ }{{$\circ$}}2
           {>>}{{>>}}2 {>>=}{{>>=}}2
           {|}{{$\mid$}}1
}

% A virtual programming language, named after Nicolas Bourbaki, a collective
% pseudonym of a group of mathematicians, including André Weil, Henri Cartan,
% Jean Dieudonné, etc.
% https://en.wikipedia.org/wiki/Nicolas_Bourbaki
% The name emphasises its virtual, ideal, and humor

\lstdefinelanguage{Bourbaki}{
  morekeywords={abstract, case, catch, class, struct,
    and, or, not, data, void, to,
    do, else, false, for, if, new, null, return,
    throw, true, try, type, val, var, while, yield },
  otherkeywords={=>,<-,<\%,<:,>:,\#,@},
  literate={+}{{$+$}}1 {/}{{$/$}}1 {*}{{$*$}}1 {=}{{$=$}}1
           {>}{{$>$}}1 {<}{{$<$}}1 {\\}{{$\lambda$}}1
           {\\\\}{{\char`\\\char`\\}}1
           {!=}{{$\neq$}}2
           {->}{{$\rightarrow$}}2 {>=}{{$\geq$}}2 {<-}{{$\leftarrow$}}2
           {<=}{{$\leq$}}2 {=>}{{$\Rightarrow$}}2
           {\ .}{{$\circ$}}2 {\ .\ }{{$\circ$}}2
           {|}{{$\mid$}}1,
  sensitive = true,
  morecomment=[l]{//},
  morecomment=[n]{/*}{*/},
  morestring=[b]",
  morestring=[b]'
}

\lstloadlanguages{C, C++, Java, Lisp, Haskell, Python, Bourbaki}

\lstset{
  basicstyle = \footnotesize\ttfamily,
  commentstyle = \normalsize\rmfamily,
  texcl = true,
  showstringspaces = false,
  upquote = true,
  flexiblecolumns = false
}

\lstnewenvironment{Haskell}[1][]
{\lstset{
    language = Haskell,
    style=Haskell,
    #1}}{}

\newcommand{\arrowto}[2][]{\xlongrightarrow[#1]{\displaystyle #2}}
\newcommand{\lbb}{\llparenthesis} % left banana bracket
\newcommand{\rbb}{\rrparenthesis} % right banana bracket

\newcommand{\nil}{\varnothing} % short for \varnothing

\newcommand{\lbparen}{%
  \mathopen{%
    \sbox0{$()$}%
    \setlength{\unitlength}{\dimexpr\ht0+\dp0}%
    \raisebox{-\dp0}{%
      \begin{picture}(.32,1)
      \linethickness{\fontdimen8\textfont3}
      \roundcap
      \put(0,0){\raisebox{\depth}{$($}}
      \polyline(0.32,0)(0,0)(0,1)(0.32,1)
      \end{picture}%
    }%
  }%
}

\newcommand{\rbparen}{%
  \mathclose{%
    \sbox0{$()$}%
    \setlength{\unitlength}{\dimexpr\ht0+\dp0}%
    \raisebox{-\dp0}{%
      \begin{picture}(.32,1)
      \linethickness{\fontdimen8\textfont3}
      \roundcap
      \put(-0.08,0){\raisebox{\depth}{$)$}}
      \polyline(0,0)(0.32,0)(0.32,1)(0,1)
      \end{picture}%
    }%
  }%
}

\newcommand{\llb}{\lbparen} % left lens bracket
\newcommand{\rlb}{\rbparen} % right lens bracket

% ======================================================================

\def\BibTeX{{\rm B\kern-.05em{\sc i\kern-.025em b}\kern-.08em
    T\kern-.1667em\lower.7ex\hbox{E}\kern-.125emX}}

%
% mathematics
%
\newcommand{\be}{\begin{equation}}
\newcommand{\ee}{\end{equation}}

% numbered equation array
\newcommand{\bea}{\begin{eqnarray}}
\newcommand{\eea}{\end{eqnarray}}

% equation array not numbered
\newcommand{\bean}{\begin{eqnarray*}}
\newcommand{\eean}{\end{eqnarray*}}

% begin/end reasoning
\newcommand{\bre}{\[ \begin{array}{rcll}}
\newcommand{\ere}{\end{array} \]}

% begin/end long reasoning
\newcommand{\blre}{\[ \begin{array}{cll}}
\newcommand{\elre}{\end{array} \]}

% common functions
\DeclareMathOperator{\gcm}{gcm}
\DeclareMathOperator{\fold}{fold}

\newcommand{\btab}{\vspace{5mm} \begin{tabular}}
\newcommand{\etab}{\end{tabular} \vspace{5mm}}

\makeatletter
\newcommand{\verbatimfont}[1]{\renewcommand{\verbatim@font}{\ttfamily#1}}
\makeatother

\setcounter{tocdepth}{4}
%\setcounter{secnumdepth}{4}

%\verbatimfont{\footnotesize}

% For Chinese Language support
\DeclareOption{cn}{

  % detect and select Chinese font
  % ------------------------------
  % fc-list :lang=zh    % list all Chinese fonts
  % fc-list :mono       % list all mono fonts
  % fc-cache            % refresh cache to load new installed fonts

  \IfFontExistsTF{STSong}{ % Mac OS X
    \message{Apply CN font: STSong, STHeiti, STFangsong}
    \setCJKmainfont[BoldFont=STHeiti, ItalicFont=STKaiti]{STSong}
    \setCJKsansfont{STHeiti}
    \setCJKmonofont{STFangsong}
  }{
    \IfFontExistsTF{SimSun}{ % Windows
      \message{Apply CN font: SimSun, SimHei}
      \setCJKmainfont{SimSun}
      \setCJKsansfont{SimHei}
    }{
      \IfFontExistsTF{Noto Serif CJK SC}{ % Linux
        \message{Apply CN font: Noto CJK}
        \setCJKmainfont{Noto Serif CJK SC} % (思源宋体)
        \setCJKsansfont{Noto Sans CJK SC}  % (思源黑体)
        \setCJKmonofont{Noto Sans Mono CJK SC}
      }{
        \message{Apply CN font: xeCJK default Fandol}
      }
    }
  }

  \XeTeXlinebreaklocale "zh"  % to solve the line breaking issue
  \XeTeXlinebreakskip = 0pt plus 1pt minus 0.1pt

  \newtheorem{axiom}{公理}[section]
  \newtheorem{theorem}{定理}[section]
  \newtheorem{lemma}[theorem]{引理}
  \newtheorem{proposition}[theorem]{命题}
  \newtheorem{corollary}[theorem]{推论}
  \newtheorem{definition}{定义}[section]
  \newtheorem{example}{例}[section]

  % 中文书籍设置
  % ====================================
  \renewcommand\ExerciseName{练习}
}

\DeclareOption{en}{
  \newtheorem{axiom}{Axiom}[section]
  \newtheorem{theorem}{Theorem}[section]
  \newtheorem{lemma}[theorem]{Lemma}
  \newtheorem{proposition}[theorem]{Proposition}
  \newtheorem{corollary}[theorem]{Corollary}
  \newtheorem{definition}{Definition}[section]
  \newtheorem{example}{Example}[section]
}


\DeclareOption*{\PackageWarning{prelude}{Unknown ‘\CurrentOption’}}
\ProcessOptions\relax
